<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="手机唯一标识符" prop="phoneId">
        <el-input
          v-model="queryParams.phoneId"
          placeholder="请输入手机唯一标识符"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="空闲存储空间 (字节)" prop="storageFree">
        <el-input
          v-model="queryParams.storageFree"
          placeholder="请输入空闲存储空间 (字节)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总存储空间 (字节)" prop="storageTotal">
        <el-input
          v-model="queryParams.storageTotal"
          placeholder="请输入总存储空间 (字节)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="核心温度 (°C)" prop="coreTemp">
        <el-input
          v-model="queryParams.coreTemp"
          placeholder="请输入核心温度 (°C)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="APD管理器版本" prop="appApdManager">
        <el-input
          v-model="queryParams.appApdManager"
          placeholder="请输入APD管理器版本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Hook应用版本" prop="appHook">
        <el-input
          v-model="queryParams.appHook"
          placeholder="请输入Hook应用版本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="脚本扩展版本" prop="appScriptExtend">
        <el-input
          v-model="queryParams.appScriptExtend"
          placeholder="请输入脚本扩展版本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="局域网IP地址" prop="ipLan">
        <el-input
          v-model="queryParams.ipLan"
          placeholder="请输入局域网IP地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="互联网IP地址" prop="ipInternet">
        <el-input
          v-model="queryParams.ipInternet"
          placeholder="请输入互联网IP地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="WebView版本" prop="appWebview">
        <el-input
          v-model="queryParams.appWebview"
          placeholder="请输入WebView版本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="节点精灵脚本版本" prop="appScriptNodeSprite">
        <el-input
          v-model="queryParams.appScriptNodeSprite"
          placeholder="请输入节点精灵脚本版本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="ROM版本" prop="appRom">
        <el-input
          v-model="queryParams.appRom"
          placeholder="请输入ROM版本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="APD服务器版本" prop="appApdServer">
        <el-input
          v-model="queryParams.appApdServer"
          placeholder="请输入APD服务器版本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="APD守护进程版本" prop="appApdDeamon">
        <el-input
          v-model="queryParams.appApdDeamon"
          placeholder="请输入APD守护进程版本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="GAID" prop="gaid">
        <el-input
          v-model="queryParams.gaid"
          placeholder="请输入GAID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备型号" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入设备型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电池电量百分比" prop="batteryLevel">
        <el-input
          v-model="queryParams.batteryLevel"
          placeholder="请输入电池电量百分比"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Chrome浏览器版本" prop="appChrome">
        <el-input
          v-model="queryParams.appChrome"
          placeholder="请输入Chrome浏览器版本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="平台名称" prop="platform">
        <el-input
          v-model="queryParams.platform"
          placeholder="请输入平台名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌名称" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入品牌名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联的用户令牌 (来自 busy_apkey 表)" prop="apkey">
        <el-input
          v-model="queryParams.apkey"
          placeholder="请输入关联的用户令牌 (来自 busy_apkey 表)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分组ID" prop="groupId">
        <el-input
          v-model="queryParams.groupId"
          placeholder="请输入分组ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mobile:mobile:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mobile:mobile:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mobile:mobile:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mobile:mobile:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mobileList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键，自增" align="center" prop="id" />
      <el-table-column label="手机唯一标识符" align="center" prop="phoneId" />
      <el-table-column label="空闲存储空间 (字节)" align="center" prop="storageFree" />
      <el-table-column label="总存储空间 (字节)" align="center" prop="storageTotal" />
      <el-table-column label="核心温度 (°C)" align="center" prop="coreTemp" />
      <el-table-column label="APD管理器版本" align="center" prop="appApdManager" />
      <el-table-column label="Hook应用版本" align="center" prop="appHook" />
      <el-table-column label="已安装应用列表 (JSON格式: {"包名": "版本号", ...})" align="center" prop="apps" />
      <el-table-column label="脚本扩展版本" align="center" prop="appScriptExtend" />
      <el-table-column label="局域网IP地址" align="center" prop="ipLan" />
      <el-table-column label="互联网IP地址" align="center" prop="ipInternet" />
      <el-table-column label="WebView版本" align="center" prop="appWebview" />
      <el-table-column label="节点精灵脚本版本" align="center" prop="appScriptNodeSprite" />
      <el-table-column label="ROM版本" align="center" prop="appRom" />
      <el-table-column label="APD服务器版本" align="center" prop="appApdServer" />
      <el-table-column label="APD守护进程版本" align="center" prop="appApdDeamon" />
      <el-table-column label="GAID" align="center" prop="gaid" />
      <el-table-column label="设备型号" align="center" prop="model" />
      <el-table-column label="电池电量百分比" align="center" prop="batteryLevel" />
      <el-table-column label="Chrome浏览器版本" align="center" prop="appChrome" />
      <el-table-column label="平台名称" align="center" prop="platform" />
      <el-table-column label="品牌名称" align="center" prop="brand" />
      <el-table-column label="关联的用户令牌 (来自 busy_apkey 表)" align="center" prop="apkey" />
      <el-table-column label="分组ID" align="center" prop="groupId" />
      <el-table-column label="设备状态 (0: 离线, 1: 在线)" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mobile:mobile:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mobile:mobile:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="手机唯一标识符" prop="phoneId">
          <el-input v-model="form.phoneId" placeholder="请输入手机唯一标识符" />
        </el-form-item>
        <el-form-item label="空闲存储空间 (字节)" prop="storageFree">
          <el-input v-model="form.storageFree" placeholder="请输入空闲存储空间 (字节)" />
        </el-form-item>
        <el-form-item label="总存储空间 (字节)" prop="storageTotal">
          <el-input v-model="form.storageTotal" placeholder="请输入总存储空间 (字节)" />
        </el-form-item>
        <el-form-item label="核心温度 (°C)" prop="coreTemp">
          <el-input v-model="form.coreTemp" placeholder="请输入核心温度 (°C)" />
        </el-form-item>
        <el-form-item label="APD管理器版本" prop="appApdManager">
          <el-input v-model="form.appApdManager" placeholder="请输入APD管理器版本" />
        </el-form-item>
        <el-form-item label="Hook应用版本" prop="appHook">
          <el-input v-model="form.appHook" placeholder="请输入Hook应用版本" />
        </el-form-item>
        <el-form-item label="已安装应用列表 (JSON格式: {"包名": "版本号", ...})" prop="apps">
          <el-input v-model="form.apps" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="脚本扩展版本" prop="appScriptExtend">
          <el-input v-model="form.appScriptExtend" placeholder="请输入脚本扩展版本" />
        </el-form-item>
        <el-form-item label="局域网IP地址" prop="ipLan">
          <el-input v-model="form.ipLan" placeholder="请输入局域网IP地址" />
        </el-form-item>
        <el-form-item label="互联网IP地址" prop="ipInternet">
          <el-input v-model="form.ipInternet" placeholder="请输入互联网IP地址" />
        </el-form-item>
        <el-form-item label="WebView版本" prop="appWebview">
          <el-input v-model="form.appWebview" placeholder="请输入WebView版本" />
        </el-form-item>
        <el-form-item label="节点精灵脚本版本" prop="appScriptNodeSprite">
          <el-input v-model="form.appScriptNodeSprite" placeholder="请输入节点精灵脚本版本" />
        </el-form-item>
        <el-form-item label="ROM版本" prop="appRom">
          <el-input v-model="form.appRom" placeholder="请输入ROM版本" />
        </el-form-item>
        <el-form-item label="APD服务器版本" prop="appApdServer">
          <el-input v-model="form.appApdServer" placeholder="请输入APD服务器版本" />
        </el-form-item>
        <el-form-item label="APD守护进程版本" prop="appApdDeamon">
          <el-input v-model="form.appApdDeamon" placeholder="请输入APD守护进程版本" />
        </el-form-item>
        <el-form-item label="GAID" prop="gaid">
          <el-input v-model="form.gaid" placeholder="请输入GAID" />
        </el-form-item>
        <el-form-item label="设备型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入设备型号" />
        </el-form-item>
        <el-form-item label="电池电量百分比" prop="batteryLevel">
          <el-input v-model="form.batteryLevel" placeholder="请输入电池电量百分比" />
        </el-form-item>
        <el-form-item label="Chrome浏览器版本" prop="appChrome">
          <el-input v-model="form.appChrome" placeholder="请输入Chrome浏览器版本" />
        </el-form-item>
        <el-form-item label="平台名称" prop="platform">
          <el-input v-model="form.platform" placeholder="请输入平台名称" />
        </el-form-item>
        <el-form-item label="品牌名称" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌名称" />
        </el-form-item>
        <el-form-item label="关联的用户令牌 (来自 busy_apkey 表)" prop="apkey">
          <el-input v-model="form.apkey" placeholder="请输入关联的用户令牌 (来自 busy_apkey 表)" />
        </el-form-item>
        <el-form-item label="分组ID" prop="groupId">
          <el-input v-model="form.groupId" placeholder="请输入分组ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMobile, getMobile, delMobile, addMobile, updateMobile } from "@/api/mobile/mobile"

export default {
  name: "Mobile",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备管理表格数据
      mobileList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phoneId: null,
        storageFree: null,
        storageTotal: null,
        coreTemp: null,
        appApdManager: null,
        appHook: null,
        apps: null,
        appScriptExtend: null,
        ipLan: null,
        ipInternet: null,
        appWebview: null,
        appScriptNodeSprite: null,
        appRom: null,
        appApdServer: null,
        appApdDeamon: null,
        gaid: null,
        model: null,
        batteryLevel: null,
        appChrome: null,
        platform: null,
        brand: null,
        apkey: null,
        groupId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        phoneId: [
          { required: true, message: "手机唯一标识符不能为空", trigger: "blur" }
        ],
        apkey: [
          { required: true, message: "关联的用户令牌 (来自 busy_apkey 表)不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询设备管理列表 */
    getList() {
      this.loading = true
      listMobile(this.queryParams).then(response => {
        this.mobileList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        phoneId: null,
        storageFree: null,
        storageTotal: null,
        coreTemp: null,
        appApdManager: null,
        appHook: null,
        apps: null,
        appScriptExtend: null,
        ipLan: null,
        ipInternet: null,
        appWebview: null,
        appScriptNodeSprite: null,
        appRom: null,
        appApdServer: null,
        appApdDeamon: null,
        gaid: null,
        model: null,
        batteryLevel: null,
        appChrome: null,
        platform: null,
        brand: null,
        apkey: null,
        groupId: null,
        status: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加设备管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getMobile(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改设备管理"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMobile(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addMobile(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除设备管理编号为"' + ids + '"的数据项？').then(function() {
        return delMobile(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mobile/mobile/export', {
        ...this.queryParams
      }, `mobile_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
